﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Windows.Media.Effects;
using System.Windows.Media.Animation;

namespace WGM_Client_Net35
{
    /// <summary>
    /// Interaction logic for LoginWindow.xaml
    /// </summary>
    public partial class LoginWindow : Window
    {        
        ConnectionHandlerObject _cho = new ConnectionHandlerObject();
        
        public LoginWindow()
        {
            InitializeComponent();
            _loginBtn.IsDefault = true;
            _passwordTB.IsKeyboardFocusWithinChanged += new DependencyPropertyChangedEventHandler(_passwordTB_IsKeyboardFocusWithinChanged);
            _usernameTb.IsKeyboardFocusWithinChanged += new DependencyPropertyChangedEventHandler(_usernameTb_IsKeyboardFocusWithinChanged);
            if (_rememberChk.IsChecked == true)
            {
                _passwordTB.Password = Properties.Settings.Default.LastLoginPassword;
                _usernameTb.Text = Properties.Settings.Default.LastLoginName;
            }
        }

        void _usernameTb_IsKeyboardFocusWithinChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            TextBox box = sender as TextBox;
            if ((bool)e.NewValue == true)
            {
                box.Background = new SolidColorBrush(Colors.LightYellow);
            }
            else
            {
                box.Background = new SolidColorBrush(Colors.White);
            }
        }

        void _passwordTB_IsKeyboardFocusWithinChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            PasswordBox box = sender as PasswordBox;
            if ((bool)e.NewValue == true)
            {
                box.Background = new SolidColorBrush(Colors.LightYellow);
            }
            else
            {
                box.Background = new SolidColorBrush(Colors.White);            
            }
        }

        private void _loginBtn_Click(object sender, RoutedEventArgs e)
        {
            if (_usernameTb.Text.Trim() == "")
            {
                DoubleAnimation anim = new DoubleAnimation(1,0,new Duration(new TimeSpan(0,0,0,0,100)));
                anim.AutoReverse = true;
                anim.RepeatBehavior =new RepeatBehavior(3);
                _usernameTb.BeginAnimation(TextBox.OpacityProperty, anim);
                
                
                return;
            }
            foreach (UIElement control in _allControls.Children)
            {
                try
                {
                    control.IsEnabled = false;
                }
                catch { }
            }
            
            _cancelBtn.IsEnabled = true;
            BackgroundWorker bg = new BackgroundWorker();
            bg.DoWork += new DoWorkEventHandler(bg_DoWork);
            bg.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bg_RunWorkerCompleted);
            bg.RunWorkerAsync(new string[]{_usernameTb.Text,_passwordTB.Password});

        }

        void bg_DoWork(object sender, DoWorkEventArgs e)
        {
            string[] creds = e.Argument as string[];
            _cho.LogOn(creds[0], creds[1]);
        }
        void bg_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            foreach (UIElement control in _allControls.Children)
            {
                try
                {
                    
                    control.IsEnabled = true;
                }
                catch { }
            }
            if (_cho.IsConnected)
            {
                MainWindow mainWindow = new MainWindow();
                mainWindow.Show();
                mainWindow.SetConnectionHandler(_cho);
                if (Properties.Settings.Default.RememberLogin == true)
                {
                    Properties.Settings.Default.LastLoginName = _usernameTb.Text;
                    Properties.Settings.Default.LastLoginPassword = _passwordTB.Password;
                }
                else
                {
                    Properties.Settings.Default.LastLoginName = "";
                    Properties.Settings.Default.LastLoginPassword = "";
                }
                this.Close();
            }
            else
            {
                MessageBox.Show("Login error\r\n"+_cho.LastServerMessage,"Wayne's Global Messenger Beta",MessageBoxButton.OK,MessageBoxImage.Error);
            }

        }



        private void _cancelBtn_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown();
        }

        private void textBlock3_MouseDown(object sender, MouseButtonEventArgs e)
        {
            CreateAccountWindow caw = new CreateAccountWindow();
            if(caw.ShowDialog() == true)
                this.Close();

        }

        private void _rememberChk_Checked(object sender, RoutedEventArgs e)
        {

        }
    }
}
